

#include <atomic>
#include <iostream>
#include <thread>

#include "trigger_scheduler.hpp"

static std::atomic_bool cond {false};
void func () {
    auto start     = std::chrono::high_resolution_clock::now ();
    auto last_time = 0;
    while (1) {
        std::this_thread::sleep_for (std::chrono::microseconds (300));
        auto end      = std::chrono::high_resolution_clock::now ();
        auto duration = std::chrono::duration_cast<std::chrono::milliseconds> (end - start);
        if (duration.count () - last_time >= 3000) {
            last_time = duration.count ();
            cond      = !cond;
        }
    }
}

auto                    my_cond = [] () -> bool { return cond.load (std::memory_order_acquire); };

int main (const int _argc, const char * const _argv[], const char * const _envp[]) {
    uint64_t t = 0;

    [[maybe_unused]] uint32_t t1 {0};
    [[maybe_unused]] uint32_t t2 {0};
    [[maybe_unused]] uint32_t t3 {0};
    [[maybe_unused]] uint32_t t4 {0};
    [[maybe_unused]] uint32_t t5 {0};
    [[maybe_unused]] uint32_t t6 {0};
    [[maybe_unused]] uint32_t t7 {0};
    [[maybe_unused]] uint32_t t8 {0};
    [[maybe_unused]] uint32_t t9 {0};
    [[maybe_unused]] uint32_t t10 {0};
    [[maybe_unused]] uint32_t t11 {0};

    std::thread {func}.detach ();

    Thread_Pool_ct pool {

            Thread_Pool_ct::Pool_Config_st {
                    ._queue_max      = 200,
                    ._thread_max     = 8,
                    ._thread_min     = 2,
                    ._thread_step    = 1,
                    ._auto_threshold = false,
            }

    };

    pool.start ();

    Trigger_Scheduler_ct sc {&pool};


    // // 固定周期无限次数
    // [[maybe_unused]] auto id1 = sc.registerTask (

    //         [&t, &t1] () {
    //             std::printf ("hello 1 for repeat INF: %u at time %lu\n", t1++, t);
    //         },
    //         Trigger_Scheduler_ct::SET_INTERVAL (100),
    //         Trigger_Scheduler_ct::SET_REPEAT (Trigger_Scheduler_ct::K_REPEAT_INFINITE),
    //         Trigger_Scheduler_ct::K_ZEROTH_CYCLE_ON
    // );


    // // 固定周期有限次数
    // [[maybe_unused]] auto id2 = sc.registerTask (

    //         [&t, &t2] () {
    //             std::printf ("hello 2 for repeat 3: %u at time %lu\n", t2++, t);
    //         },
    //         Trigger_Scheduler_ct::SET_INTERVAL (100),
    //         Trigger_Scheduler_ct::SET_REPEAT (3),
    //         Trigger_Scheduler_ct::K_ZEROTH_CYCLE_ON

    // );


    // // 固定周期 ET 无限次
    // [[maybe_unused]] auto id3 = sc.registerTask (

    //         [&t, &t3] () {
    //             std::printf ("hello 3 for cond ET repeat INF: %u at time %lu\n", t3++, t);
    //         },
    //         Trigger_Scheduler_ct::SET_INTERVAL (100),
    //         Trigger_Scheduler_ct::SET_REPEAT (Trigger_Scheduler_ct::K_REPEAT_INFINITE),
    //         Trigger_Scheduler_ct::K_ZEROTH_CYCLE_OFF,
    //         Trigger_Scheduler_ct::SET_CONDITION (my_cond),
    //         Trigger_Scheduler_ct::K_CONDITION_ET

    // );


    // // 固定周期 ET 有限次
    // [[maybe_unused]] auto id4 = sc.registerTask (

    //         [&t, &t4] () {
    //             std::printf ("hello 4 for cond ET repeat 3: %u at time %lu\n", t4++, t);
    //         },
    //         Trigger_Scheduler_ct::SET_INTERVAL (100),
    //         Trigger_Scheduler_ct::SET_REPEAT (3),
    //         Trigger_Scheduler_ct::K_ZEROTH_CYCLE_OFF,
    //         Trigger_Scheduler_ct::SET_CONDITION (my_cond),
    //         Trigger_Scheduler_ct::K_CONDITION_ET

    // );


    // // 固定周期 LT 无限次
    // [[maybe_unused]] auto id5 = sc.registerTask (

    //         [&t, &t5] () {
    //             std::printf ("hello 5 for cond LT repeat INF: %u at time %lu\n", t5++, t);
    //         },
    //         Trigger_Scheduler_ct::SET_INTERVAL (100),
    //         Trigger_Scheduler_ct::SET_REPEAT (Trigger_Scheduler_ct::K_REPEAT_INFINITE),
    //         Trigger_Scheduler_ct::K_ZEROTH_CYCLE_OFF,
    //         Trigger_Scheduler_ct::SET_CONDITION (my_cond),
    //         Trigger_Scheduler_ct::K_CONDITION_LT

    // );


    // // 固定周期 LT 有限次
    // [[maybe_unused]] auto id6 = sc.registerTask (

    //         [&t, &t6] () {
    //             std::printf ("hello 6 for cond LT repeat 90: %u at time %lu\n", t6++, t);
    //         },
    //         Trigger_Scheduler_ct::SET_INTERVAL (100),
    //         Trigger_Scheduler_ct::SET_REPEAT (90),
    //         Trigger_Scheduler_ct::K_ZEROTH_CYCLE_OFF,
    //         Trigger_Scheduler_ct::SET_CONDITION (my_cond),
    //         Trigger_Scheduler_ct::K_CONDITION_LT

    // );


    // // 无固定周期 ET 无限次
    // [[maybe_unused]] auto id7 = sc.registerTask (

    //         [&t, &t7] () {
    //             std::printf ("hello 7 for cond only ET repeat INF: %u at time %lu\n", t7++, t);
    //         },
    //         Trigger_Scheduler_ct::SET_INTERVAL (Trigger_Scheduler_ct::K_NO_INTERVAL),
    //         Trigger_Scheduler_ct::SET_REPEAT (Trigger_Scheduler_ct::K_REPEAT_INFINITE),
    //         Trigger_Scheduler_ct::K_ZEROTH_CYCLE_OFF,
    //         Trigger_Scheduler_ct::SET_CONDITION (my_cond),
    //         Trigger_Scheduler_ct::K_CONDITION_ET

    // );


    // // 无固定周期 ET 有限次
    // [[maybe_unused]] auto id8 = sc.registerTask (

    //         [&t, &t8] () {
    //             std::printf ("hello 8 for cond only ET repeat 3: %u at time %lu\n", t8++, t);
    //         },
    //         Trigger_Scheduler_ct::SET_INTERVAL (Trigger_Scheduler_ct::K_NO_INTERVAL),
    //         Trigger_Scheduler_ct::SET_REPEAT (3),
    //         Trigger_Scheduler_ct::K_ZEROTH_CYCLE_OFF,
    //         Trigger_Scheduler_ct::SET_CONDITION (my_cond),
    //         Trigger_Scheduler_ct::K_CONDITION_ET

    // );


    // // 无固定周期 LT 无限次
    // [[maybe_unused]] auto id9 = sc.registerTask (

    //         [&t, &t9] () {
    //             std::printf ("hello 9 for cond only LT repeat INF: %u at time %lu\n", t9++, t);
    //         },
    //         Trigger_Scheduler_ct::SET_INTERVAL (Trigger_Scheduler_ct::K_NO_INTERVAL),
    //         Trigger_Scheduler_ct::SET_REPEAT (Trigger_Scheduler_ct::K_REPEAT_INFINITE),
    //         Trigger_Scheduler_ct::K_ZEROTH_CYCLE_OFF,
    //         Trigger_Scheduler_ct::SET_CONDITION (my_cond),
    //         Trigger_Scheduler_ct::K_CONDITION_LT

    // );


    // // 无固定周期 LT 有限次
    // [[maybe_unused]] auto id10 = sc.registerTask (

    //         [&t, &t10] () {
    //             std::printf ("hello 10 for cond only LT repeat 3: %u at time %lu\n", t10++, t);
    //         },
    //         Trigger_Scheduler_ct::SET_INTERVAL (Trigger_Scheduler_ct::K_NO_INTERVAL),
    //         Trigger_Scheduler_ct::SET_REPEAT (3),
    //         Trigger_Scheduler_ct::K_ZEROTH_CYCLE_OFF,
    //         Trigger_Scheduler_ct::SET_CONDITION (my_cond),
    //         Trigger_Scheduler_ct::K_CONDITION_LT

    // );


    // 演示使用 builder 创建任务
    [[maybe_unused]] auto id11 = sc.registerTask (

            Trigger_Scheduler_ct::Task_Builder_st {}
                    .enabled (true)
                    .action (
                            [&t, &t11] () {
                                std::printf ("builder: hello 11 for cond only ET repeat INF: %u at time %lu\n", t11++, t);
                            })
                    .interval (Trigger_Scheduler_ct::K_NO_INTERVAL)
                    .repeat (Trigger_Scheduler_ct::K_REPEAT_INFINITE)
                    .zeroth_cycle (Trigger_Scheduler_ct::K_ZEROTH_CYCLE_OFF)
                    .condition (my_cond)
                    .condition_type (Trigger_Scheduler_ct::K_CONDITION_ET)
                    .build ()

    );


    auto start = std::chrono::high_resolution_clock::now ();

    while (1) {
        auto end      = std::chrono::high_resolution_clock::now ();
        auto duration = std::chrono::duration_cast<std::chrono::milliseconds> (end - start);
        t             = duration.count ();
        sc.process (t);


        // if (t == 1001)
        //     sc.unregisterTask (id2);
    }
    return 0;
}
